package com.example.androidexample.recyclerview;

import android.content.Context;
import android.util.Log;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.androidexample.R;
import com.example.basic.base.BaseRecyclerViewAdapter;

import java.util.ArrayList;

public class SuperRvAdapter extends BaseRecyclerViewAdapter<String> {

    private static final String TAG = "SuperRvAdapter";
    private int mStyle = 0;

    private RecyclerView.LayoutManager layoutManager = null;

    private Context mContext;
    private RecyclerView mRecyclerView;

    private ArrayList<String> stringData = new ArrayList<>();

    public SuperRvAdapter(Context context) {
        Log.d(TAG, "SuperRvAdapter: ");
        mContext = context;
    }

    public void setData(ArrayList<String> datas) {
        Log.d(TAG, "setData: ");
        stringData = datas;
    }

    public void onItemDismiss(int position) {
        if (position < 0 || position > getItemCount()) {
            return;
        }

        stringData.remove(position);
        notifyItemRemoved(position);

        // 解决 RecyclerView 删除 Item 导致位置错乱的问题
        if (position != stringData.size()) {
            notifyItemRangeChanged(position, stringData.size() - position);
        }
    }

    public void onItemMove(int fromPosition, int toPosition) {
        Log.d(TAG, "onItemMove: fromPosition = " + fromPosition + " toPosition = " + toPosition);
        String prev = stringData.remove(fromPosition);
        Log.d(TAG, "onItemMove: ------------------------");
        for (String s : stringData) {
            Log.d(TAG, "onItemMove: " + s);
        }
        Log.d(TAG, "onItemMove: ------------------------");
        stringData.add(toPosition, prev);
        for (String s : stringData) {
            Log.d(TAG, "onItemMove: " + s);
        }
        Log.d(TAG, "onItemMove: ------------------------");
        notifyItemMoved(fromPosition, toPosition);
//        notifyDataSetChanged();
        for (String s : stringData) {
            Log.d(TAG, "onItemMove: " + s);
        }
    }

    /*
    reverseLayout  表示是否需要倒叙绘制
     */
    public void setLayoutStyle(int style, RecyclerView recyclerView) {
        Log.d(TAG, "setLayoutStyle: ");
        mStyle = style;
        mRecyclerView = recyclerView;
        buildLayoutManager();

    }

    private void buildLayoutManager() {
        switch (mStyle) {
            case 0:  //竖直
                layoutManager = new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false);
                break;
            case 1:  //水平
                layoutManager = new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false);
                break;
            case 2:  //网格 固定行
                layoutManager = new GridLayoutManager(mContext, 2, RecyclerView.HORIZONTAL, false);
                break;
            case 3:  //网格 固定列
                layoutManager = new GridLayoutManager(mContext, 2, RecyclerView.VERTICAL, false);
                break;
            case 4:  //瀑布
                layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
                break;
        }
        mRecyclerView.setLayoutManager(layoutManager);
    }

    public void updateLayoutStyle(int style) {
        Log.d(TAG, "updateLayoutStyle: ");
        mStyle = style;
        buildLayoutManager();
        mRecyclerView.setAdapter(this);
    }

    @Override
    public int getLayoutId(int viewType) {
        int layoutId = 0;
        Log.d(TAG, "getLayoutId: ");
        switch (mStyle) {
            case 0:  //竖直
            case 3:  //网格 固定列
                layoutId = R.layout.activity_recyclerview_item_vertical;
                break;
            case 1:  //水平
            case 2:  //网格 固定行
                layoutId = R.layout.activity_recyclerview_item_horizontal;
                break;
            case 4:  //瀑布
                layoutId = R.layout.activity_recyclerview_item_staggeredgridl;
                break;

        }
        return layoutId;
    }

    @Override
    protected String getDataByPosition(int position) {
        Log.d(TAG, "getDataByPosition: ");
        return stringData.get(position);
    }

    @Override
    protected int getTotalSize() {
        Log.d(TAG, "getTotalSize: ");
        return stringData.size();
    }

    @Override
    public void convert(VH holder, String data, int position) {
        holder.<TextView>getView(R.id.item_tv).setText(data);
    }
}
